home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 6860 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.8 KB

  1. Path: maverick.tad.eds.com!news-admin@tad.eds.com
  2. From: gulleha@vaxixhp3.vaxix.slg.eds.com (Harold Guller)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: simple code, argc, argv, strcmp()
  5. Date: 15 Feb 1996 20:11:11 GMT
  6. Organization: EDS
  7. Message-ID: <4g040v$7jj@maverick.tad.eds.com>
  8. References: <11f7cc$17261a.3b3@daprez> <9602011151.AA04526@dxmint.cern.ch>
  9. Reply-To: gulleha@vaxixhp3.vaxix.sig.eds.com
  10. NNTP-Posting-Host: va057.vaxix.slg.eds.com
  11. X-Newsreader: WinVN 0.91.3
  12.  
  13. In article <9602011151.AA04526@dxmint.cern.ch>, danpop@mail.cern.ch (Dan Pop) says:
  14. >>int main (int argc, char **argv) {
  15. >>
  16. >>  if (!strcmp(argv[1],"-d") || !strcmp(argv[1],"-e")) {
  17. >>    Usage();
  18. >>  }
  19. >...
  20. >>
  21. >>Can someone see what's wrong with this ?
  22. >>am I not using strcmp() right ?
  23. >
  24. >Right :-)
  25. >
  26. >Your code is equivalent to:
  27. >
  28. >    if (strcmp(argv[1],"-d") == 0 || strcmp(argv[1],"-e") == 0) Usage();
  29. >
  30. >Which means that any time the program is correctly invoked, it will call
  31. >Usage!
  32. >
  33. >So:
  34. >
  35. >1. Never use ! with strcmp.  It's a good way to shoot yourself in the foot.
  36.  
  37. You can say that about anything that someone does not understad.
  38. A better solution is to learn the concept.  The "!" operator is widely
  39. used and should be understood.
  40.  
  41.  
  42. >2. Always check what a function is supposed to return before using it.
  43. >   strcmp returns 0 when the strings are equal.
  44.  
  45. >3. Are you sure you didn't mean && when you wrote ||?  Even if !strcmp()
  46. >   would have worked as you thought it did, the || operator would have
  47. >   caused the expression inside the if statement to _always_ evaluate to 1.
  48.  
  49. Why do you advise learning the "&&" and "||" conditions?  These are more
  50. complex than the "!" operator, and he did "shoot himself in the foot".
  51. Shoudn't he use nested if else contrcuts to achieve his purpose?
  52.                                                                          
  53.  
  54.                                                                          
  55.                                                                          
  56.                                                                          
  57.                                                                          
  58.  
  59.  
  60.                                                                          
  61.  
  62.                                                                          
  63.                                                                          
  64.                                                                          
  65.                                                                          
  66.  
  67.  
  68.                                                                          
  69.  
  70.                                                                          
  71.                                                                          
  72.                                                                          
  73.                                                                          
  74.  
  75.  
  76.